package com.etrans.web.gps.business.report.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.domain.EnterpriseQcVO;
import com.etrans.web.gps.business.report.domain.EnterpriseSwap;
import com.etrans.web.gps.business.report.service.EnterpiseAnalyzesService;

/**
 * @author RenweiWu
 * @date 2017年8月30日
 * @param:   
 */
@Controller
@RequestMapping("/base/report/enterpriseAnalyzes")
public class EnterpiseAnalyzesController extends BaseController{

	//private static final Logger logger = LoggerFactory.getLogger(EnterpiseAnalyzesController.class);
	
	@Autowired
	private EnterpiseAnalyzesService enterpriseAnalyzesService;
	
	/**
	 * 跳转业户综合分析页面
	 * @return
	 */
	@RequestMapping("toEnterpriseAnalyzes.html")
	public String toEnterpriseAnalyzes(){
		return "views/business/report/enterpriseAnalyzes";
	}
	
	/**
	 * 点击业户考核跳转业户综合分析页面
	 * @return
	 */
	@RequestMapping("toEnterpriseAnalyzesAL.html")
	public String toEnterpriseAnalyzesAL(String enterprise_id,String timeType,String inputTime,Model model){
		model.addAttribute("enterprise_id",enterprise_id);
		model.addAttribute("timeType",timeType);
		model.addAttribute("inputTime",inputTime);
		return "views/business/report/enterpriseAnalyzesAL";
	}
	
	/**
	 * 跳转业户考核页面
	 * @return
	 */
	@RequestMapping("toEnterpriseAnalyzesQC.html")
	public String toEnterpriseAnalyzesQC(){
		return "views/business/report/enterpriseAnalyzesQC";
	}
	
	/**
	 * 加载业户下拉框
	 * @return
	 */
	@RequestMapping("queryEnterprise.html")
	public @ResponseBody List<HashMap<String, Object>> queryEnterprise(String enterprise_name){
		int areaId= this.getSysUser().getArea_id();
		List<HashMap<String,Object>> list = enterpriseAnalyzesService.queryEnterAndPlatByRole(areaId,enterprise_name);
		return list;
	}

	/**
	 * 业户综合分析加载信息
	 * @return
	 */
	@RequestMapping("queryEnterpriseSwap1.html")
	public void queryEnterpriseSwap1(HttpServletRequest request,HttpServletResponse response,String enterprise_id,String timeType,String inputTime){
		Integer queryType = Integer.valueOf(timeType);
		if (queryType>0 && inputTime != null && !"".equals(inputTime)) {
			List<Integer> areaIdList  = enterpriseAnalyzesService.getAreaIdList();
			if (areaIdList == null || areaIdList.size() <= 0) {
				int areaId= this.getSysUser().getArea_id();	
				areaIdList = enterpriseAnalyzesService.queryAreaIdListByRole(areaId);
			}
			List<HashMap<String, Object>> result = enterpriseAnalyzesService.queryEnterpriseSwap1(areaIdList,enterprise_id,queryType,inputTime);
			this.ajaxResultJsonWithGzip(request, response, result==null?"":result);		}
	}
	
	@RequestMapping("queryEnterpriseSwap2.html")
	public void queryEnterpriseSwap2(HttpServletRequest request,HttpServletResponse response,String enterprise_id,String timeType,String inputTime){
		Integer queryType = Integer.valueOf(timeType);
		if (queryType>0 && inputTime != null && !"".equals(inputTime)) {
			List<Integer> areaIdList  = enterpriseAnalyzesService.getAreaIdList();
			if (areaIdList == null || areaIdList.size() <= 0) {
				int areaId= this.getSysUser().getArea_id();	
				areaIdList = enterpriseAnalyzesService.queryAreaIdListByRole(areaId);
			}
			List<HashMap<String, Object>> result = enterpriseAnalyzesService.queryEnterpriseSwap2(areaIdList,enterprise_id,queryType,inputTime);
			this.ajaxResultJsonWithGzip(request, response, result==null?"":result);		}
	}
	
	@RequestMapping("queryEnterpriseSwap3.html")
	public void queryEnterpriseSwap3(HttpServletRequest request,HttpServletResponse response,String enterprise_id,String timeType,String inputTime){
		Integer queryType = Integer.valueOf(timeType);
		if (queryType>0 && inputTime != null && !"".equals(inputTime)) {
			List<Integer> areaIdList  = enterpriseAnalyzesService.getAreaIdList();
			if (areaIdList == null || areaIdList.size() <= 0) {
				int areaId= this.getSysUser().getArea_id();	
				areaIdList = enterpriseAnalyzesService.queryAreaIdListByRole(areaId);
			}
			List<HashMap<String, Object>> result = enterpriseAnalyzesService.queryEnterpriseSwap3(areaIdList,enterprise_id,queryType,inputTime);
			this.ajaxResultJsonWithGzip(request, response, result==null?"":result);		}
	}
	
	@RequestMapping("queryEnterpriseSwap4.html")
	public void queryEnterpriseSwap4(HttpServletRequest request,HttpServletResponse response,String enterprise_id,String timeType,String inputTime){
		Integer queryType = Integer.valueOf(timeType);
		if (queryType>0 && inputTime != null && !"".equals(inputTime)) {
			List<Integer> areaIdList  = enterpriseAnalyzesService.getAreaIdList();
			if (areaIdList == null || areaIdList.size() <= 0) {
				int areaId= this.getSysUser().getArea_id();	
				areaIdList = enterpriseAnalyzesService.queryAreaIdListByRole(areaId);
			}
			List<HashMap<String, Object>> result = enterpriseAnalyzesService.queryEnterpriseSwap4(areaIdList,enterprise_id,queryType,inputTime);
			this.ajaxResultJsonWithGzip(request, response, result==null?"":result);
		}
	}
	/**
	 * 业户考核分析加载信息
	 * @return
	 */
	@RequestMapping("queryEnterpriseQc.html")
	public void queryEnterpriseQc(HttpServletRequest request,HttpServletResponse response,String enterprise_id,
			String timeType,String inputTime){
		Integer queryType = Integer.valueOf(timeType);
		if (queryType>0 && inputTime != null && !"".equals(inputTime)) {
			List<Integer> areaIdList  = enterpriseAnalyzesService.getAreaIdList();
			if (areaIdList == null || areaIdList.size() <= 0) {
				int areaId= this.getSysUser().getArea_id();	
				areaIdList = enterpriseAnalyzesService.queryAreaIdListByRole(areaId);
			}
			List<EnterpriseQcVO> result = enterpriseAnalyzesService.queryEnterpriseQc(areaIdList,enterprise_id,queryType,inputTime);
			this.ajaxResultJsonWithGzip(request, response, result==null?"":result);
		}
	}

	/**
	 * 业户综合分析表中点击业户显示相应的柱状图
	 * @return
	 */
	@RequestMapping("queryBarChart.html")
	public @ResponseBody List<EnterpriseSwap> queryBarChart(String enterprise_id,String timeType,String inputTime){
		Integer queryType = Integer.valueOf(timeType);
		Integer enterpriseId = Integer.valueOf(enterprise_id);
		if (queryType>0 && inputTime != null && !"".equals(inputTime)) {
			return enterpriseAnalyzesService.queryBarChart(enterpriseId,queryType,inputTime);
		}
		return null;
	}

	/**
	 * 导出业户综合分析Excel
	 * @param paramsGrid  条件
	 * @param colModel	     表头
	 * @param title		      标题
	 * @param imgsrc      导出分析图
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("export.html")
	public void export(HttpServletRequest request, HttpServletResponse response,String tableDataStr,
			String tableDataTtiles,String sheetNames, String title,String imgsrc){
		List<HashMap<String, Object>> colModelList = new ArrayList<HashMap<String, Object>>();
   		List<String> imgsrcList   = JSON.parseObject(imgsrc, List.class);
   		List<String> tableDataTtilesList   = JSON.parseObject(tableDataTtiles, List.class);
   		List<String> tableDataStrList   = JSON.parseObject(tableDataStr, List.class);
   		List<String> sheetList   = JSON.parseObject(sheetNames, List.class);
   		List rowsList = new ArrayList<>();
   		if(tableDataStrList.size()>0&&!"".equals(tableDataStrList.get(0))){
   			HashMap<String, Object> tempHash=new HashMap<String, Object>();
   			String columnArray ="a,b,c,d,e,f";
   			String widthArray ="250,100,100,100,100,100";
   			String[] columnArrSpt=columnArray.split(",");
   			tempHash.put("titleArray", tableDataTtilesList.get(0).split(","));
   			tempHash.put("columnArray",columnArrSpt);
   			tempHash.put("widthArray", widthArray.split(","));
   			colModelList.add(tempHash);
   			String[] tepmrows = tableDataStrList.get(0).split("\\&");
   			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
   			for (String qcInfo : tepmrows) {
   				try
   				{
   					HashMap<String,Object> tpHash=new HashMap<String,Object>();
   					String[] infoArray = qcInfo.split("\\#");
   					for(int i=0;i<columnArrSpt.length;i++){
   						tpHash.put(columnArrSpt[i], infoArray[i]);
   					}
   					tempList.add(tpHash);
   				
   				} catch (Exception e){
   					e.printStackTrace();
   				}
   			}
   			rowsList.add(tempList);
   		}
   		if(tableDataStrList.size()>0&&!"".equals(tableDataStrList.get(1))){
   			HashMap<String, Object> tempHash=new HashMap<String, Object>();
   			String columnArray ="a,b,c,d";
   			String widthArray ="250,250,250,250";
   			String[] columnArrSpt=columnArray.split(",");
   			tempHash.put("titleArray", tableDataTtilesList.get(1).split(","));
   			tempHash.put("columnArray",columnArrSpt);
   			tempHash.put("widthArray", widthArray.split(","));
   			colModelList.add(tempHash);
   			String[] tepmrows = tableDataStrList.get(1).split("\\&");
   			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
   			for (String qcInfo : tepmrows) {
   				try
   				{
   					HashMap<String,Object> tpHash=new HashMap<String,Object>();
   					String[] infoArray = qcInfo.split("\\#");
   					for(int i=0;i<columnArrSpt.length;i++){
   						tpHash.put(columnArrSpt[i], infoArray[i]);
   					}
   					tempList.add(tpHash);
   				
   				} catch (Exception e){
   					e.printStackTrace();
   				}
   			}
   			rowsList.add(tempList);
   		}
   		if(tableDataStrList.size()>0&&!"".equals(tableDataStrList.get(2))){
   			HashMap<String, Object> tempHash=new HashMap<String, Object>();
   			String columnArray ="a,b,c,d,e,f,g,h,i,j";
   			String widthArray ="240,90,90,90,90,100,100,110,130,140";
   			String[] columnArrSpt=columnArray.split(",");
   			tempHash.put("titleArray", tableDataTtilesList.get(2).split(","));
   			tempHash.put("columnArray",columnArrSpt);
   			tempHash.put("widthArray", widthArray.split(","));
   			colModelList.add(tempHash);
   			String[] tepmrows = tableDataStrList.get(2).split("\\&");
   			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
   			for (String qcInfo : tepmrows) {
   				try
   				{
   					HashMap<String,Object> tpHash=new HashMap<String,Object>();
   					String[] infoArray = qcInfo.split("\\#");
   					for(int i=0;i<columnArrSpt.length;i++){
   						tpHash.put(columnArrSpt[i], infoArray[i]);
   					}
   					tempList.add(tpHash);
   				
   				} catch (Exception e){
   					e.printStackTrace();
   				}
   			}
   			rowsList.add(tempList);
   		}
   		if(tableDataStrList.size()>0&&!"".equals(tableDataStrList.get(3))){
   			HashMap<String, Object> tempHash=new HashMap<String, Object>();
   			String columnArray ="a,b,c,d,e,f,g,h";
   			String widthArray ="240,100,100,100,100,100,110,110";
   			String[] columnArrSpt=columnArray.split(",");
   			tempHash.put("titleArray", tableDataTtilesList.get(3).split(","));
   			tempHash.put("columnArray",columnArrSpt);
   			tempHash.put("widthArray", widthArray.split(","));
   			colModelList.add(tempHash);
   			String[] tepmrows = tableDataStrList.get(3).split("\\&");
   			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
   			for (String qcInfo : tepmrows) {
   				try
   				{
   					HashMap<String,Object> tpHash=new HashMap<String,Object>();
   					String[] infoArray = qcInfo.split("\\#");
   					for(int i=0;i<columnArrSpt.length;i++){
   						tpHash.put(columnArrSpt[i], infoArray[i]);
   					}
   					tempList.add(tpHash);
   				
   				} catch (Exception e){
   					e.printStackTrace();
   				}
   			}
   			rowsList.add(tempList);
   		}
   		exportExls(request, response,title, colModelList,rowsList,imgsrcList,sheetList);
	}
	
	/**
	 * 导出业户考核Excel
	 * @param title		      标题
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("QCexport.html")
	public void QCexport(HttpServletRequest request, HttpServletResponse response, String title, String tableDataStr,
			String tableDataTtiles, String time){
		List<HashMap<String, Object>> colModelList = new ArrayList<HashMap<String, Object>>();
   		List rowsList = new ArrayList<>();
   		if(!"".equals(tableDataStr)){
   			HashMap<String, Object> tempHash=new HashMap<String, Object>();
   			String columnArray ="a,b,c,d,e,f,g,h,i,j,k,l,m";
   			String widthArray ="80,60,60,240,90,90,120,120,155,110,155,130,120";
   			String[] columnArrSpt=columnArray.split(",");
   			tempHash.put("titleArray", tableDataTtiles.split(","));
   			tempHash.put("columnArray",columnArrSpt);
   			tempHash.put("widthArray", widthArray.split(","));
   			colModelList.add(tempHash);
   			String[] tepmrows = tableDataStr.split("\\&");
   			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
   			for (String qcInfo : tepmrows) {
   				try
   				{
   					HashMap<String,Object> tpHash=new HashMap<String,Object>();
   					String[] infoArray = qcInfo.split("\\#");
   					for(int i=0;i<columnArrSpt.length;i++){
   						tpHash.put(columnArrSpt[i], infoArray[i]);
   					}
   					tempList.add(tpHash);
   				
   				} catch (Exception e){
   					e.printStackTrace();
   				}
   			}
   			rowsList.add(tempList);
   		}
   		exportExl(request, response,title, colModelList, rowsList, null);
	}
}
